package com.sherlocky.back2oss.job;

import com.alibaba.fastjson.JSON;
import com.sherlocky.back2oss.service.BackupService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * XxlJob开发示例（Bean模式）
 * <p>
 * 开发步骤：
 * 1、任务开发：在Spring Bean实例中，开发Job方法；
 * 2、注解配置：为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")"，注解value值对应的是调度中心新建任务的JobHandler属性的值。
 * 3、执行日志：需要通过 "XxlJobHelper.log" 打印执行日志；
 * 4、任务结果：默认任务结果为 "成功" 状态，不需要主动设置；如有诉求，比如设置任务结果为失败，可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果；
 */
@Slf4j
@Component
public class BackupXxlJob {
    @Autowired
    private BackupService backupService;
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @XxlJob("ossBackupJobHandler")
    public void ossBackupJobHandler() throws Exception {
        XxlJobHelper.log("XXL-JOB, Hello World.");
        XxlJobHelper.log("### 备份文件开始：当前时间：" + dateFormat.format(new Date()));
        backupService.backup();
        XxlJobHelper.log("### 备份文件结束：" + dateFormat.format(new Date()));
    }

    @XxlJob("ossClearOldsJobHandler")
    public void ossClearOldsJobHandler() throws Exception {
        XxlJobHelper.log("### 清理上月过期备份文件开始：当前时间：" + dateFormat.format(new Date()));
        List<String> needDeleteFiles = backupService.deleteLastMonthOldFiles();
        XxlJobHelper.log(JSON.toJSONString(needDeleteFiles));
        XxlJobHelper.log("### 清理上月过期备份文件结束：" + dateFormat.format(new Date()));
    }
}
